home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 443 < prev    next >
Encoding:
Text File  |  1996-08-06  |  4.2 KB  |  108 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: phalpern@truffle.ultranet.com (Pablo Halpern)
  3. Newsgroups: comp.std.c++
  4. Subject: Explicit should apply to conversion operators
  5. Date: 22 Feb 1996 09:18:15 PST
  6. Organization: UltraNet Communications, Inc.
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4gi5mh$v7o@caesar.ultra.net>
  9. References: <4gg7df$76c@charnel.ecst.csuchico.edu> <4gg8p5$lcq@engnews1.Eng.Sun.COM>
  10. NNTP-Posting-Host: isolde.mti.sgi.com
  11. X-Original-Date: Thu, 22 Feb 1996 16:30:06 GMT
  12. X-Newsreader: Forte Agent .99b.113
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMSylZ0y4NqrwXLNJAQHhQQH/b2L1/pGp//CJuKExpzkCm/prDMFeyqjQ
  15.     ukQCZE0FcJx/HnVIb1k7yxqQg6uLnKKb6S5QwduwYBRy2RiDek0jTg==
  16.     =pzTL
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. clamage@Eng.Sun.COM (Steve Clamage) wrote:
  20.  
  21. >Whether "explicit" should also apply to type conversion operators
  22. >has been discussed here and in the C++ committee. Some people on
  23. >the committee thought that the proposal to add "explicit" for
  24. >constructors also applied to conversion operators, but the
  25. >proposal did not contain that wording.
  26. >
  27. >Personally, I don't see much utility in "explicit" for conversion
  28. >operators. If you don't want a type conversion to be implicit, don't
  29. >use a conversion operator, use a named function instead. Example:
  30. > [ example deleted ]
  31. >You don't have this option with constructors, and the workarounds for
  32. >the lack of "explicit" constructors are often ugly.
  33. >
  34. >The only advantage I can see for an "explicit" conversion operator is
  35. >that a name for a complicated type, especially one involving pointers
  36. >or references, might be inconvenient and not intuitive. In that
  37. >case you might prefer to be able to cast to char**& instead of thinking
  38. >up a name for that type.
  39.  
  40. I used to believe this, also, but two things changed my mind:
  41.  
  42. The first is templates. When creating a template function or class, one
  43. might want to specify that a class used for template instantiation has
  44. the requirement that it be explicitly convertable to, say, int or char*
  45. or String. For example:
  46.  
  47.   LinkList<string> traceLog;
  48.  
  49.   template <class T>
  50.   Trace(const T& t) { traceLog.append(static_cast<string> t); }
  51.  
  52.   class X { ... explicit operator string() const; ... }
  53.   X x;
  54.   Trace(x);
  55.   string y;
  56.   Trace y;
  57.  
  58. This would not work if the only way to convert from an X to a string was
  59. to call and as_string() function, since not all types that are
  60. convertable to String would have an as_string() function, expecially not
  61. class string itself!  This goes double for built-in types, where we
  62. don't have the option of adding class members.
  63.  
  64. The second, slightly less significant reason is to provide a little bit
  65. more syntactic sugar when building types that work like the built-in
  66. ones.  For example:
  67.  
  68.   // Complex number class
  69.   class Complex
  70.   {
  71.     Complex(double);  // implicit conversion from double
  72.     Complex(double, double);
  73.     as_double() const;  // explicit conversion to double
  74.     explicit operator double() const;  // perferred conversion notation
  75.     ...
  76.   };
  77.  
  78.   f1(Complex);
  79.   f1(4.5);  // Ok, implicit conversion
  80.   f2(double);
  81.   Complex c(1.2, 3.4);  
  82.   f2(c);    // Error: no implicit conversion to double
  83.   f2(c.as_double());  // OK, but doesn't work like built-in types
  84.   f2(double(c));      // explicit conversion, works like built-in types
  85.  
  86. The above would work if we didn't declare the conversion operator
  87. explicit, but then we could have accidental loss of information if a
  88. complex number were passed into a function requiring a double. The use
  89. of an explicit cast makes it clear that the programmer knows what she/he
  90. is doing, and it uses a normal cast syntax.
  91.  
  92. Given the existance of the "explicit" keyword, wouldn't it be a
  93. reasonable addition to make it work for conversion operators as well as
  94. for constructors?
  95.  
  96. -------------------------------------------------------------
  97. Pablo Halpern                   phalpern@truffle.ultranet.com
  98.  
  99. I am self-employed. Therefore, my opinions *do* represent 
  100. those of my employer.
  101. ---
  102. [ To submit articles: Try just posting with your newsreader.  If that fails,
  103.                       use mailto:std-c++@ncar.ucar.edu
  104.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  105.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  106.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  107. ]
  108.